/**
 * @description: EasyReader
 * @author: candy.d.chen
 * @LastEditors: candy.d.chen
 * @LastEditTime: 2021/11/15 11:11
 */
import * as jQuery from 'jQuery'
//@ sourceURL=EasyReader.min.js
const EasyReader = function (args, finish) {
  this.finish = finish;
  this.args = args;
  this.init = jQuery.proxy(this.init, this);
  this.isEnable = jQuery.proxy(this.isEnable, this);
  this.readable = jQuery.proxy(this.readable, this);
  this.isLoaded = jQuery.proxy(this.isLoaded, this);
  this.read = jQuery.proxy(this.read, this);
  this.pause = jQuery.proxy(this.pause, this);
  this.resume = jQuery.proxy(this.resume, this);
  this.stop = jQuery.proxy(this.stop, this);
  this.volumePlus = jQuery.proxy(this.volumePlus, this);
  this.volumeMinus = jQuery.proxy(this.volumeMinus, this);
  this.volume = jQuery.proxy(this.volume, this);
  this.canVolumePlus = jQuery.proxy(this.canVolumePlus, this);
  this.canVolumeMinus = jQuery.proxy(this.canVolumeMinus, this);
  this.ratePlus = jQuery.proxy(this.ratePlus, this);
  this.rateMinus = jQuery.proxy(this.rateMinus, this);
  this.rate = jQuery.proxy(this.rate, this);
  this.canRatePlus = jQuery.proxy(this.canRatePlus, this);
  this.canRateMinus = jQuery.proxy(this.canRateMinus, this);
  this.onFinish = jQuery.proxy(this.onFinish, this);
  this.init()
};
EasyReader.readableExp = new RegExp("[a-zA-Z0-9\u4E00-\u9FA5\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A]");
EasyReader.readable = function (text) {
  return EasyReader.readableExp.test(text)
};
EasyReader.prototype = {
  init: function () {
    try {
      this._reader = new EasyReader.MSTTS(this.args, this.onFinish)
    } catch (e) {
      this._reader = new EasyReader.ServerTTS(this.args, this.onFinish)
    }
  },
  isEnable: function () {
    return !!this._reader
  },
  readable: function (text) {
    return EasyReader.readableExp.test(text)
  },
  isLoaded: function() {
    return this._reader.isLoaded();
  },
  read: function (text) {
    text += "";
    if (this._reader && text && EasyReader.readableExp.test(text)) {
      this._reader.read(text)
    }
  },
  pause: function () {
    if (this._reader) {
      this._reader.pause()
    }
  },
  resume: function () {
    if (this._reader) {
      this._reader.resume()
    }
  },
  stop: function () {
    if (this._reader) {
      this._reader.stop()
    }
  },
  volumePlus: function () {
    if (this._reader) {
      this._reader.volumePlus()
    }
  },
  volumeMinus: function () {
    if (this._reader) {
      this._reader.volumeMinus()
    }
  },
  volume: function (v) {
    if (this._reader) {
      return this._reader.volume(v)
    }
  },
  canVolumePlus: function () {
    if (this._reader) {
      return this._reader.canVolumePlus()
    } else {
      return false
    }
  },
  canVolumeMinus: function () {
    if (this._reader) {
      return this._reader.canVolumeMinus()
    } else {
      return false
    }
  },
  ratePlus: function () {
    if (this._reader) {
      this._reader.ratePlus()
    }
  },
  rateMinus: function () {
    if (this._reader) {
      this._reader.rateMinus()
    }
  },
  rate: function (v) {
    if (this._reader) {
      return this._reader.rate(v)
    }
  },
  canRatePlus: function () {
    if (this._reader) {
      return this._reader.canRatePlus()
    } else {
      return false
    }
  },
  canRateMinus: function () {
    if (this._reader) {
      return this._reader.canRateMinus()
    } else {
      return false
    }
  },
  onFinish: function () {
    if (jQuery.isFunction(this.finish)) {
      this.finish()
    }
  }
};
EasyReader.MSTTS = function (args, finish) {
  this._args = args;
  this._finish = finish;
  this._timer = null;
  this.init = jQuery.proxy(this.init, this);
  this.read = jQuery.proxy(this.read, this);
  this.pause = jQuery.proxy(this.pause, this);
  this.resume = jQuery.proxy(this.resume, this);
  this.stop = jQuery.proxy(this.stop, this);
  this.volumePlus = jQuery.proxy(this.volumePlus, this);
  this.volumeMinus = jQuery.proxy(this.volumeMinus, this);
  this.volume = jQuery.proxy(this.volume, this);
  this.canVolumePlus = jQuery.proxy(this.canVolumePlus, this);
  this.canVolumeMinus = jQuery.proxy(this.canVolumeMinus, this);
  this.ratePlus = jQuery.proxy(this.ratePlus, this);
  this.rateMinus = jQuery.proxy(this.rateMinus, this);
  this.rate = jQuery.proxy(this.rate, this);
  this.canRatePlus = jQuery.proxy(this.canRatePlus, this);
  this.canRateMinus = jQuery.proxy(this.canRateMinus, this);
  this.checkState = jQuery.proxy(this.checkState, this);
  this.init()
};
EasyReader.MSTTS.prototype = {
  init: function () {
    // 创建FileSystemObject对象实例
    this._reader = new ActiveXObject("Sapi.SpVoice");
    this._reader.Voice = this._reader.GetVoices("Name = Microsoft Lili").Item(0);
    if (this._args && this._args.speed && this._args.speed == "low") {
      this.rate(1)
    }

  },
  checkState: function () {
    clearTimeout(this._timer);
    if (this._reader.Status.RunningState == 2) {
      this._timer = setTimeout(this.checkState, 200)
    } else {
      clearTimeout(this._timer);
      this._finish()
    }
  },
  read: function (text) {
    this._reader.Speak(text, 1);
    this._timer = setTimeout(this.checkState, 200)
  },
  pause: function () {
    this._reader.Pause()
  },
  resume: function () {
    this._reader.Resume()
  },
  stop: function () {
    clearTimeout(this._timer);
    this._reader.Speak("", 2)
  },
  volumePlus: function () {
    if (this._reader.Volume >= 100) {
      this._reader.Volume = 100
    } else {
      this._reader.Volume += 20
    }
  },
  volumeMinus: function () {
    if (this._reader.Volume <= 0) {
      this._reader.Volume = 0
    } else {
      this._reader.Volume -= 20
    }
  },
  volume: function (v) {
    if (v) {
      if (v > 1) {
        v = 1
      }
      if (v < 0) {
        v = 0
      }
      this._reader.Volume = v * 100
    } else {
      return this._reader.Volume / 100
    }
  },
  canVolumePlus: function () {
    return this._reader.Volume < 100
  },
  canVolumeMinus: function () {
    return this._reader.Volume > 0
  },
  ratePlus: function () {
    if (this._reader.Rate >= 10) {
      this._reader.Rate = 10
    } else {
      this._reader.Rate += 1
    }
  },
  rateMinus: function () {
    if (this._reader.Rate <= -10) {
      this._reader.Rate = -10
    } else {
      this._reader.Rate -= 1
    }
  },
  rate: function (v) {
    if (v) {
      if (v > 10) {
        v = 10
      }
      if (v < -10) {
        v = -10
      }
      this._reader.Rate = v
    } else {
      return this._reader.Rate
    }

  },
  canRatePlus: function () {
    return this._reader.Rate < 10
  },
  canRateMinus: function () {
    return this._reader.Rate > -10
  }
};
EasyReader.ServerTTS = function (args, finish) {
  this._args = args;
  this._finish = finish;
  this.readerVolume = 1;
  this.init = jQuery.proxy(this.init, this);
  this.read = jQuery.proxy(this.read, this);
  this.pause = jQuery.proxy(this.pause, this);
  this.resume = jQuery.proxy(this.resume, this);
  this.stop = jQuery.proxy(this.stop, this);
  this.volumePlus = jQuery.proxy(this.volumePlus, this);
  this.volumeMinus = jQuery.proxy(this.volumeMinus, this);
  this.volume = jQuery.proxy(this.volume, this);
  this.canVolumePlus = jQuery.proxy(this.canVolumePlus, this);
  this.canVolumeMinus = jQuery.proxy(this.canVolumeMinus, this);
  this.ratePlus = jQuery.proxy(this.ratePlus, this);
  this.rateMinus = jQuery.proxy(this.rateMinus, this);
  this.rate = jQuery.proxy(this.rate, this);
  this.canRatePlus = jQuery.proxy(this.canRatePlus, this);
  this.canRateMinus = jQuery.proxy(this.canRateMinus, this);
  this.onFinish = jQuery.proxy(this.onFinish, this);
  this.onError = jQuery.proxy(this.onError, this);
  this.init()
};
EasyReader.ServerTTS.prototype = {
  init: function () {
    this.player = jQuery("<div id='jquery_jplayer'></div>");
    jQuery(document.body).append(this.player);
    this.player.jPlayer({
      swfPath: this._args && this._args.playerPath ? this._args.playerPath : "/js/",
      supplied: "wav,mp3",
      wmode: "window",
      volume: this.readerVolume
    });
    this.player.bind(jQuery.jPlayer.event.ended, this.onFinish);
    this.player.bind(jQuery.jPlayer.event.error, this.onError)
  },
  onFinish: function () {
    if (jQuery.isFunction(this._finish)) {
      this._finish()
    }
  },
  onError: function () {
    if (this.mp3) {
      try {
        this.player.jPlayer("setMedia", {
          mp3: this.mp3 + "&time=" + new Date().getTime()
        }).jPlayer("play");
        this.mp3 = ""
      } catch (e) {
        console.log('candy--打印:e', e)
      }
    }
  },
  isLoaded: function() {
    var status = this.player.jPlayer("getStatus");
    return (!status.srcSet && status.readyState == 0) || (status.srcSet && status.readyState == 4);
  },
  read: function (text) {
    var md5 = jQuery.md5.hex_md5(text);
    var TTSUrl = this._args.TTSUrl;
    if(TTSUrl != null && TTSUrl != ''){
      this.mp3 = TTSUrl + "interface/trans/txt2voice.do?text=" + encodeURI(text) + "&download=1&spd=" + md5;
      try {
        this.player.jPlayer("setMedia", {
          mp3: this.mp3
        }).jPlayer("play")
      } catch (e) {
        console.log('candy--打印:e', e)
      }
    }
  },
  pause: function () {
    this.player.jPlayer("pause")
  },
  resume: function () {
    this.player.jPlayer("play")
  },
  stop: function () {
    try {
      this.player.jPlayer("stop")
    } catch (e) {
      console.log('candy--打印:e', e)
    }
  },
  volumePlus: function () {
    this.readerVolume += 0.1;
    if (this.readerVolume >= 0.99) {
      this.readerVolume = 1
    }
    this.player.jPlayer("volume", this.readerVolume)
  },
  volumeMinus: function () {
    this.readerVolume -= 0.1;
    if (this.readerVolume <= 0.01) {
      this.readerVolume = 0
    }
    this.player.jPlayer("volume", this.readerVolume)
  },
  volume: function (v) {
    if (v) {
      if (v > 1) {
        v = 1
      }
      if (v < 0) {
        v = 0
      }
      this.player.jPlayer("volume", v)
    } else {
      return this.readerVolume
    }
  },
  canVolumePlus: function () {
    return this.readerVolume < 1
  },
  canVolumeMinus: function () {
    return this.readerVolume > 0
  },
  ratePlus: function () {
    this.readerRate += 10;
    if (this.readerRate >= 10) {
      this.readerRate = 10
    }
    this.player.jPlayer("PlaybackRate", this.readerRate)
  },
  rateMinus: function () {
    this.readerRate -= 1;
    if (this.readerRate <= -10) {
      this.readerRate = 0
    }
    this.player.jPlayer("PlaybackRate", this.readerRate)
  },
  rate: function (v) {
    if (v) {
      if (v > 10) {
        v = 10
      }
      if (v < -10) {
        v = -10
      }
      this.player.jPlayer("PlaybackRate", v);
      this._args.speed = v;
    } else {
      return this.readerRate;
    }
  },
  canRatePlus: function () {
    return this.readerRate < 10
  },
  canRateMinus: function () {
    return this.readerRate > -10
  }
};

export default {
  EasyReader
}
